package org.erp.store.strategy.impl;

import lombok.extern.slf4j.Slf4j;
import org.erp.common.enums.DeviceInventoryEvent;
import org.erp.store.domain.dto.BatchDeviceInventoryDto;
import org.erp.store.domain.dto.BatchDeviceInventoryResponseDto;
import org.springframework.stereotype.Component;

/**
 * 库存调拨事件处理
 */
@Slf4j
@Component
public class AllocateInventoryEventStrategy extends BaseInventoryEventStrategy {
    @Override
    public DeviceInventoryEvent getEvent() { return DeviceInventoryEvent.ALLOCATE; }

    @Override
    public BatchDeviceInventoryResponseDto execute(BatchDeviceInventoryDto dto) {
        return batchUpdate(dto, (inv, invDto) -> {
            inv.setTransferUserId(dto.getTransferUserId());
            inv.setTransferUserName(dto.getTransferUserName());
            inv.setDeptId(dto.getDeptId());
            inv.setDeptName(dto.getDeptName());
            if (invDto != null && invDto.getProcurementPremium() != null) {
                inv.setProcurementPremium(invDto.getProcurementPremium());
            }
            Integer transferNum = inv.getTransferNum() == null ? 0 : inv.getTransferNum();
            inv.setTransferNum(transferNum + 1);
        }, defaultAfterPersistHook);
    }
}
